package com.iss.supermarket.GoodsInfoManagerUI;

import com.iss.supermarket.util.MessageWindow.MsgWin;
import com.iss.supermarket.util.entity.Goods;
import com.iss.supermarket.util.network.NetWork;
import com.iss.supermarket.util.repository.GoodsDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

public class GoodsInfoManagerUI extends JFrame{
    //单例模式
    private static GoodsInfoManagerUI goodsInfoManagerUI;
    //定义组件
    private JPanel goodsInfoManagePanel;
    private JLabel titleLabel;
    private JLabel[] goodsLabels;
    private JTextField[] goodsTextFields;
    private JButton jButton1,jButton2,jButton3;
    private JComboBox<String> BarCodeComboBox;

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    GoodsDao goodsDao = ctx.getBean("GoodsDao", GoodsDao.class);

    public GoodsInfoManagerUI(){
        InitAssembly();
        InitUI();
    }

    /**
     * InitUI___初始化界面,完成组件向界面的添加
     */
    private void InitUI(){
//        Container container = getContentPane();
        Box verticalBox = Box.createVerticalBox();

        JPanel jPanel0 = new JPanel();
        jPanel0.add(titleLabel);
        verticalBox.add(jPanel0);

        JPanel jPanel1 = new JPanel();
        jPanel1.add(goodsLabels[0]);
        jPanel1.add(BarCodeComboBox);
        verticalBox.add(jPanel1);

        for(int i=1;i<goodsLabels.length;++i){
            JPanel tmpPanel = new JPanel();
            tmpPanel.add(goodsLabels[i]);
            tmpPanel.add(goodsTextFields[i]);
            verticalBox.add(tmpPanel);
        }

        JPanel jPanel2 = new JPanel();
        jPanel2.add(jButton1);
        jPanel2.add(jButton2);
        jPanel2.add(jButton3);
        verticalBox.add(jPanel2);

        goodsInfoManagePanel.add(verticalBox);

//        container.add(goodsInfoManagePanel);
//        setSize(600,600);
//        setVisible(true);
    }

    /**
     * InitAssembly___初始化组件,完成组件的实例化和事件监听的添加
     */
    private void InitAssembly(){
        titleLabel = new JLabel("商品信息管理");
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setFont(new Font("宋体",Font.BOLD,20));

        goodsLabels = new JLabel[8];
        goodsLabels[0] = new JLabel("条形码：");
        goodsLabels[1] = new JLabel("商品名称：");
        goodsLabels[2] = new JLabel("价格：");
        goodsLabels[3] = new JLabel("单位：");
        goodsLabels[4] = new JLabel("产地：");
        goodsLabels[5] = new JLabel("规格：");
        goodsLabels[6] = new JLabel("等级：");
        goodsLabels[7] = new JLabel("商品数量：");

        for(int i=0;i<goodsLabels.length;++i){
            goodsLabels[i].setHorizontalAlignment(SwingConstants.LEFT);
            goodsLabels[i].setPreferredSize(new Dimension(100,25));
            goodsLabels[i].setFont(new Font("黑体",Font.PLAIN,15));
        }

        goodsTextFields = new JTextField[8];
        for(int i=0;i<8;++i){
            goodsTextFields[i] = new JTextField();
            goodsTextFields[i].setPreferredSize(new Dimension(400,25));
        }

        List<String> BarCodeList = getAllBarCode();
        BarCodeList.add(0,"无");
        BarCodeComboBox = new JComboBox<String>();
        for(int i=0;i<BarCodeList.size();++i){
            BarCodeComboBox.addItem(String.valueOf(BarCodeList.get(i)));
        }
        BarCodeComboBox.setEditable(true);
        BarCodeComboBox.setSelectedItem("");
        BarCodeComboBox.setPreferredSize(new Dimension(400,25));
        BarCodeComboBox.addItemListener(new ItemACtion());

        jButton1 = new JButton("查询");
        jButton1.addActionListener(new ButtonAction());
        jButton1.setFont(new Font("黑体",Font.PLAIN,14));
        jButton2 = new JButton("保存");
        jButton2.addActionListener(new ButtonAction());
        jButton2.setFont(new Font("黑体",Font.PLAIN,14));
        jButton3 = new JButton("删除");
        jButton3.addActionListener(new ButtonAction());
        jButton3.setFont(new Font("黑体",Font.PLAIN,14));

        goodsInfoManagePanel = new JPanel();
    }

    private class ButtonAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if("查询".equals(e.getActionCommand())){
                BarCodeComboBox.setSelectedItem(BarCodeComboBox.getSelectedItem());
                String BarCode = null;
                if(String.valueOf(BarCodeComboBox.getSelectedItem()).isEmpty()){
                    MsgWin msgWin = new MsgWin("不存在的商品二维码");
                }else{
                    BarCode = String.valueOf(BarCodeComboBox.getSelectedItem());
                }
                Goods goods = goodsDao.queryGoodsByBarCode(BarCode);
                if(goods == null || goods.getDelFlag() == 1){
                    MsgWin msgWin = new MsgWin("商品不存在");
                }else{
                    goodsTextFields[1].setText(goods.getGdBroadName());
                    goodsTextFields[2].setText(goods.getGdPrice());
                    goodsTextFields[3].setText(goods.getGdUnit());
                    goodsTextFields[4].setText(goods.getGdPlace());
                    goodsTextFields[5].setText(goods.getGdSpecs());
                    goodsTextFields[6].setText(goods.getGdLevel());
                    goodsTextFields[7].setText(String.valueOf(goods.getGdNum()));
                }

            }else if("保存".equals(e.getActionCommand())){
                goodsDao.updateGoodsInfo(goodsTextFields[0].getText(),
                        goodsTextFields[1].getText(),
                        goodsTextFields[2].getText(),
                        goodsTextFields[3].getText(),
                        goodsTextFields[4].getText(),
                        goodsTextFields[5].getText(),
                        goodsTextFields[6].getText(),
                        goodsTextFields[7].getText());
                System.out.println("保存成功");

            }else if("删除".equals(e.getActionCommand())){
                BarCodeComboBox.setSelectedItem(BarCodeComboBox.getSelectedItem());
                String BarCode = String.valueOf(BarCodeComboBox.getSelectedItem());
                Goods goods = goodsDao.queryGoodsByBarCode(BarCode);
                if(goods != null || goods.getDelFlag() != 1){
                    goods.setDelFlag(1);
                }
                goodsDao.SoftDelete(BarCode,0);
            }
        }
    }

    private class ItemACtion implements ItemListener{

        @Override
        public void itemStateChanged(ItemEvent e){
            if(e.getStateChange() == ItemEvent.SELECTED){
                // 选择的下拉框选项
                System.out.println(e.getItem());
                if(e.getItem().equals("无")){
                    BarCodeComboBox.setSelectedItem("");
                }
            }
        }
    }

    public List<String> getAllBarCode(){
        List<String> allBarCode = goodsDao.queryAllGoodsBarCode();
        return allBarCode;
    }

    public JPanel getGoodsInfoManagePanel(){
        return goodsInfoManagePanel;
    }

    public static synchronized GoodsInfoManagerUI getGoodsInfoManageUI(){
        if(goodsInfoManagerUI == null){
            goodsInfoManagerUI = new GoodsInfoManagerUI();
        }
        NetWork.getNetWork(GoodsInfoManagerUI.class.getSimpleName());
        return goodsInfoManagerUI;
    }

    public void setScanningResults(String contents){
        BarCodeComboBox.setSelectedItem(contents);

        String BarCode = null;
        if(String.valueOf(BarCodeComboBox.getSelectedItem()).isEmpty()){
            MsgWin msgWin = new MsgWin("不存在的商品二维码");
        }else{
            BarCode = String.valueOf(BarCodeComboBox.getSelectedItem());
        }
        Goods goods = goodsDao.queryGoodsByBarCode(BarCode);
        if(goods == null || goods.getDelFlag() == 1){
            MsgWin msgWin = new MsgWin("商品不存在");
        }else{
            goodsTextFields[1].setText(goods.getGdBroadName());
            goodsTextFields[2].setText(goods.getGdPrice());
            goodsTextFields[3].setText(goods.getGdUnit());
            goodsTextFields[4].setText(goods.getGdPlace());
            goodsTextFields[5].setText(goods.getGdSpecs());
            goodsTextFields[6].setText(goods.getGdLevel());
            goodsTextFields[7].setText(String.valueOf(goods.getGdNum()));
        }
    }
//    public static void main(String[] args) {
//        GoodsInfoManagerUI goodsInfoManagerUI = GoodsInfoManagerUI.getGoodsInfoManageUI();
//        List<String> abc = goodsInfoManagerUI.getAllBarCode();
//        System.out.println(abc);
//    }
}